package com.youyi.yyscreencutlibrary.Core;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

public class CutMarkView extends View {
    private static int BUTTON_EXTRA_WIDTH = 0;
    private static final int DEFAULT_VERTEX_WIDTH = 20;
    private int adjustNum = 0;
    private int cornerColor = Color.parseColor("#F0FFFFFF");
    private int cornerWidth = 20;
    private int downX;
    private int downY;
    private int endX;
    private int endY;
    private boolean isAdjustMode = false;
    private boolean isMoveMode = false;
    private RectF lbVer;
    int lineLen = 30;
    int lineWidth = 5;
    private RectF ltVer;
    private Paint mBitPaint;
    private int mHeight;
    private int mMoveHeight;
    private int mMoveWidth;
    private int mNewBottom;
    private int mNewLeft;
    private int mNewRight;
    private int mNewTop;
    private onClickListener mOnClickListener;
    private int mWidth;
    private Paint markPaint;
    private Rect markedArea;
    private int markedColor = Color.parseColor("#00000000");
    private RectF rbVer;
    private RectF rtVer;
    private int startX;
    private int startY;
    private Paint unMarkPaint;
    private int unmarkedColor = Color.parseColor("#80000000");
    private Paint vertexPaint;

    public interface onClickListener {
        void onCancel();

        void onTouch();
    }

    public CutMarkView(Context context) {
        super(context);
        init(context, null);
    }

    public CutMarkView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init(context, attributeSet);
    }

    private void init(Context context, AttributeSet attributeSet) {
        BUTTON_EXTRA_WIDTH = dp2px(context, 8.0f);
        this.cornerWidth = dp2px(context, 20.0f);
        Paint paint = new Paint();
        this.unMarkPaint = paint;
        paint.setColor(this.unmarkedColor);
        this.unMarkPaint.setAntiAlias(true);
        Paint paint2 = new Paint();
        this.markPaint = paint2;
        paint2.setColor(this.markedColor);
        this.markPaint.setStyle(Paint.Style.FILL);
        this.markPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        this.markPaint.setColor(this.markedColor);
        this.markPaint.setAntiAlias(true);
        Paint paint3 = new Paint();
        this.vertexPaint = paint3;
        paint3.setColor(this.cornerColor);
        this.vertexPaint.setAntiAlias(true);
        Paint paint4 = new Paint(1);
        this.mBitPaint = paint4;
        paint4.setFilterBitmap(true);
        this.mBitPaint.setDither(true);
        this.ltVer = new RectF();
        this.rtVer = new RectF();
        this.lbVer = new RectF();
        this.rbVer = new RectF();
        initNormal();
    }

    private void initNormal() {
        int i = getResources().getDisplayMetrics().widthPixels / 3;
        int i2 = (getResources().getDisplayMetrics().heightPixels - i) / 2;
        Rect rect = new Rect(i * 1, i2, i * 2, i + i2);
        this.markedArea = rect;
        this.startX = rect.left;
        this.startY = this.markedArea.top;
        this.endX = this.markedArea.right;
        this.endY = this.markedArea.bottom;
    }

    /* access modifiers changed from: protected */
    public void onMeasure(int i, int i2) {
        super.onMeasure(i, i2);
        int i3 = 100;
        this.mWidth = MeasureSpec.getMode(i) == 0 ? 100 : MeasureSpec.getSize(i);
        if (MeasureSpec.getMode(i2) != 0) {
            i3 = MeasureSpec.getSize(i2);
        }
        this.mHeight = i3;
    }

    /* access modifiers changed from: protected */
    public void onDraw(Canvas canvas) {
        canvas.saveLayer(0.0f, 0.0f, (float) canvas.getWidth(), (float) canvas.getHeight(), null, 31);
        canvas.drawRect(0.0f, 0.0f, (float) getWidth(), (float) getHeight(), this.unMarkPaint);
        canvas.drawRect(this.markedArea, this.markPaint);
        int i = this.lineWidth + 2;
        drawLeftTopPath(this.markedArea.left - i, 1, this.markedArea.top - i, 1, canvas);
        drawLeftTopPath(this.markedArea.right + i, -1, this.markedArea.top - i, 1, canvas);
        drawLeftTopPath(this.markedArea.left - i, 1, this.markedArea.bottom + i, -1, canvas);
        drawLeftTopPath(this.markedArea.right + i, -1, this.markedArea.bottom + i, -1, canvas);
        super.onDraw(canvas);
    }

    private void drawLeftTopPath(int i, int i2, int i3, int i4, Canvas canvas) {
        Path path = new Path();
        Path path2 = new Path();
        Path path3 = new Path();
        RectF rectF = new RectF();
        RectF rectF2 = new RectF();
        float f = (float) i;
        float f2 = (float) i3;
        rectF.set(f, f2, (float) ((this.lineLen * i2) + i), (float) ((this.lineWidth * i4) + i3));
        rectF2.set(f, f2, (float) (i + (i2 * this.lineWidth)), (float) (i3 + (i4 * this.lineLen)));
        path.rewind();
        path2.rewind();
        path3.rewind();
        path.addRect(rectF, Path.Direction.CCW);
        path2.addRect(rectF2, Path.Direction.CCW);
        path3.op(path, path2, Path.Op.UNION);
        canvas.drawPath(path3, this.vertexPaint);
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        int x = (int) motionEvent.getX();
        int y = (int) motionEvent.getY();
        int action = motionEvent.getAction();
        if (action == 0) {
            this.isAdjustMode = false;
            this.isMoveMode = false;
            this.adjustNum = 0;
            this.downX = x;
            this.downY = y;
            onClickListener onclicklistener = this.mOnClickListener;
            if (onclicklistener != null) {
                onclicklistener.onTouch();
            }
            if (isAreaContainPoint(this.ltVer, x, y)) {
                this.isAdjustMode = true;
                this.adjustNum = 1;
            } else if (isAreaContainPoint(this.rtVer, x, y)) {
                this.isAdjustMode = true;
                this.adjustNum = 2;
            } else if (isAreaContainPoint(this.lbVer, x, y)) {
                this.isAdjustMode = true;
                this.adjustNum = 3;
            } else if (isAreaContainPoint(this.rbVer, x, y)) {
                this.isAdjustMode = true;
                this.adjustNum = 4;
            } else if (this.markedArea.contains(x, y)) {
                this.isMoveMode = true;
            } else {
                this.isMoveMode = false;
                this.startX = (int) motionEvent.getX();
                int y2 = (int) motionEvent.getY();
                this.startY = y2;
                this.endX = this.startX;
                this.endY = y2;
            }
        } else if (action == 1) {
            adjustMark(x, y);
            this.startX = this.markedArea.left;
            this.startY = this.markedArea.top;
            this.endX = this.markedArea.right;
            this.endY = this.markedArea.bottom;
            onClickListener onclicklistener2 = this.mOnClickListener;
            if (onclicklistener2 != null) {
                onclicklistener2.onCancel();
            }
        } else if (action == 2) {
            adjustMark(x, y);
        }
        postInvalidate();
        return true;
    }

    private boolean isAreaContainPoint(Rect rect, int i, int i2) {
        return new Rect(rect.left - BUTTON_EXTRA_WIDTH, rect.top - BUTTON_EXTRA_WIDTH, rect.right + BUTTON_EXTRA_WIDTH, rect.bottom + BUTTON_EXTRA_WIDTH).contains(i, i2);
    }

    private boolean isAreaContainPoint(RectF rectF, int i, int i2) {
        return new RectF(rectF.left - ((float) BUTTON_EXTRA_WIDTH), rectF.top - ((float) BUTTON_EXTRA_WIDTH), rectF.right + ((float) BUTTON_EXTRA_WIDTH), rectF.bottom + ((float) BUTTON_EXTRA_WIDTH)).contains((float) i, (float) i2);
    }

    private void adjustMark(int i, int i2) {
        int i3;
        int i4;
        if (this.isAdjustMode) {
            int i5 = i - this.downX;
            int i6 = i2 - this.downY;
            int i7 = this.adjustNum;
            if (i7 == 1) {
                this.startX += i5;
                this.startY += i6;
            } else if (i7 == 2) {
                this.endX += i5;
                this.startY += i6;
            } else if (i7 == 3) {
                this.startX += i5;
                this.endY += i6;
            } else if (i7 == 4) {
                this.endX += i5;
                this.endY += i6;
            }
            this.downX = i;
            this.downY = i2;
        } else if (this.isMoveMode) {
            this.mMoveWidth = this.markedArea.width();
            this.mMoveHeight = this.markedArea.height();
            int i8 = i - this.downX;
            int i9 = i2 - this.downY;
            this.startX += i8;
            this.startY += i9;
            this.endX += i8;
            this.endY += i9;
            this.downX = i;
            this.downY = i2;
        } else {
            this.endX = i;
            this.endY = i2;
        }
        this.mNewLeft = Math.min(this.startX, this.endX);
        this.mNewTop = Math.min(this.startY, this.endY);
        this.mNewRight = Math.max(this.startX, this.endX);
        int max = Math.max(this.startY, this.endY);
        this.mNewBottom = max;
        if (this.mNewLeft < 0) {
            this.mNewLeft = 0;
        }
        int i10 = this.mNewLeft;
        int i11 = this.mWidth;
        if (i10 > i11) {
            this.mNewLeft = i11;
        }
        if (this.mNewTop < 0) {
            this.mNewTop = 0;
        }
        int i12 = this.mNewTop;
        int i13 = this.mHeight;
        if (i12 > i13) {
            this.mNewTop = i13;
        }
        if (this.mNewRight < 0) {
            this.mNewRight = 0;
        }
        if (this.mNewRight > i11) {
            this.mNewRight = i11;
        }
        if (max < 0) {
            this.mNewBottom = 0;
        }
        if (this.mNewBottom > i13) {
            this.mNewBottom = i13;
        }
        this.markedArea.set(this.mNewLeft, this.mNewTop, this.mNewRight, this.mNewBottom);
        int width = this.markedArea.width();
        int i14 = this.lineLen;
        if (width < i14 * 2) {
            int i15 = this.mNewLeft;
            int i16 = (i14 * 2) + i15;
            this.mNewRight = i16;
            int i17 = this.mWidth;
            if (i16 <= i17) {
                this.markedArea.set(i15, this.mNewTop, i16, this.mNewBottom);
            } else {
                this.mNewRight = i17;
                int i18 = i17 - (i14 * 2);
                this.mNewLeft = i18;
                this.markedArea.set(i18, this.mNewTop, i17, this.mNewBottom);
            }
        }
        int height = this.markedArea.height();
        int i19 = this.lineLen;
        if (height < i19 * 2) {
            int i20 = this.mNewTop;
            int i21 = (i19 * 2) + i20;
            this.mNewBottom = i21;
            int i22 = this.mHeight;
            if (i21 <= i22) {
                this.markedArea.set(this.mNewLeft, i20, this.mNewRight, i21);
            } else {
                this.mNewBottom = i22;
                int i23 = i22 - (i19 * 2);
                this.mNewTop = i23;
                this.markedArea.set(this.mNewLeft, i23, this.mNewRight, i22);
            }
        }
        boolean z = this.isMoveMode;
        if (z && this.mNewLeft == 0) {
            this.mNewRight = this.mMoveWidth;
        }
        if (z && this.mNewRight == (i4 = this.mWidth)) {
            this.mNewLeft = i4 - this.mMoveWidth;
        }
        if (z && this.mNewTop == 0) {
            this.mNewBottom = this.mMoveHeight;
        }
        if (z && this.mNewBottom == (i3 = this.mHeight)) {
            this.mNewTop = i3 - this.mMoveHeight;
        }
        this.markedArea.set(this.mNewLeft, this.mNewTop, this.mNewRight, this.mNewBottom);
        this.ltVer.set((float) (this.markedArea.left - (this.cornerWidth / 2)), (float) (this.markedArea.top - (this.cornerWidth / 2)), (float) (this.markedArea.left + (this.cornerWidth / 2)), (float) (this.markedArea.top + (this.cornerWidth / 2)));
        this.rtVer.set((float) (this.markedArea.right - (this.cornerWidth / 2)), (float) (this.markedArea.top - (this.cornerWidth / 2)), (float) (this.markedArea.right + (this.cornerWidth / 2)), (float) (this.markedArea.top + (this.cornerWidth / 2)));
        this.lbVer.set((float) (this.markedArea.left - (this.cornerWidth / 2)), (float) (this.markedArea.bottom - (this.cornerWidth / 2)), (float) (this.markedArea.left + (this.cornerWidth / 2)), (float) (this.markedArea.bottom + (this.cornerWidth / 2)));
        this.rbVer.set((float) (this.markedArea.right - (this.cornerWidth / 2)), (float) (this.markedArea.bottom - (this.cornerWidth / 2)), (float) (this.markedArea.right + (this.cornerWidth / 2)), (float) (this.markedArea.bottom + (this.cornerWidth / 2)));
    }

    public void setmOnClickListener(onClickListener onclicklistener) {
        this.mOnClickListener = onclicklistener;
    }

    public void setUnmarkedColor(int i) {
        this.unmarkedColor = i;
        this.unMarkPaint.setColor(i);
        invalidate();
    }

    public int dp2px(Context context, float f) {
        return (int) TypedValue.applyDimension(1, f, context.getResources().getDisplayMetrics());
    }

    public void setRect(Rect rect) {
        Rect rect2 = new Rect(rect.left, rect.top, rect.right, rect.bottom);
        this.markedArea = rect2;
        this.startX = rect2.left;
        this.startY = this.markedArea.top;
        this.endX = this.markedArea.right;
        this.endY = this.markedArea.bottom;
        this.ltVer.set((float) (this.markedArea.left - (this.cornerWidth / 2)), (float) (this.markedArea.top - (this.cornerWidth / 2)), (float) (this.markedArea.left + (this.cornerWidth / 2)), (float) (this.markedArea.top + (this.cornerWidth / 2)));
        this.rtVer.set((float) (this.markedArea.right - (this.cornerWidth / 2)), (float) (this.markedArea.top - (this.cornerWidth / 2)), (float) (this.markedArea.right + (this.cornerWidth / 2)), (float) (this.markedArea.top + (this.cornerWidth / 2)));
        this.lbVer.set((float) (this.markedArea.left - (this.cornerWidth / 2)), (float) (this.markedArea.bottom - (this.cornerWidth / 2)), (float) (this.markedArea.left + (this.cornerWidth / 2)), (float) (this.markedArea.bottom + (this.cornerWidth / 2)));
        this.rbVer.set((float) (this.markedArea.right - (this.cornerWidth / 2)), (float) (this.markedArea.bottom - (this.cornerWidth / 2)), (float) (this.markedArea.right + (this.cornerWidth / 2)), (float) (this.markedArea.bottom + (this.cornerWidth / 2)));
        postInvalidate();
    }

    public Rect getRect() {
        return this.markedArea;
    }
}
